package com.iemes.controller.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ArrangeRecordFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.mapper.hr.StaffMapper;
import com.iemes.mapper.quality.ArrangeRecordMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/quality")
@Controller
public class ArrangeRecordController extends BaseController {

	
	@Inject
	private ArrangeRecordMapper arrangeRecordMapper;
	
	@Inject
	private StaffMapper staffMapper;
	
	@RequestMapping("arrangerecord")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/quality/arrangerecord/arrangerecord_list";
	}
		
	@ResponseBody
	@RequestMapping("arrangerecord_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		ArrangeRecordFormMap arrangeRecordFormMap=getFormMap(ArrangeRecordFormMap.class);
		List<Map<String,Object>> arrangeRecordFormMaps=arrangeRecordMapper.getArrangeRecordByCondition(arrangeRecordFormMap);
		LuogicPaging(pageNow, pageSize,arrangeRecordFormMaps);
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping(value="arrangerecord_deleteEntity")
	@SystemLog(module="质检安排",methods="质检安排-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		ArrangeRecordFormMap arrangeRecordFormMap=getFormMap(ArrangeRecordFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			arrangeRecordFormMap.put("state", 1);
			arrangeRecordFormMap.put("id", str[i]);
			baseMapper.editEntity(arrangeRecordFormMap);
		}
		return "success";
	}
	
	
	@RequestMapping("arrangerecord_addUI")
	public String addUI(Model model) throws Exception {
		List<StaffFormMap> staffFormMaps=staffMapper.getAllStaffFormMap();
		model.addAttribute("staffFormMaps", staffFormMaps);
		return Common.BACKGROUND_PATH + "/quality/arrangerecord/arrangerecord_add";
	}
	
	@ResponseBody
	@RequestMapping("arrangerecord_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="质检安排",methods="质检-新增质检安排")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		ArrangeRecordFormMap arrangeRecordFormMap=getFormMap(ArrangeRecordFormMap.class);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		
		arrangeRecordFormMap.put("arrangeDate", sdf.format(date));
		//获取安排质检人
		Session session = SecurityUtils.getSubject().getSession();
		arrangeRecordFormMap.put("arrangePeople", session.getAttribute("userSessionId"));
		arrangeRecordFormMap.put("state", 0);
		arrangeRecordMapper.addEntity(arrangeRecordFormMap);
		return "success";
	}
	
	
	@RequestMapping("arrangerecord_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		ArrangeRecordFormMap arrangeRecordFormMap=arrangeRecordMapper.getArrangeRecordById(Integer.parseInt(id));
		model.addAttribute("arrangeRecordFormMap",arrangeRecordFormMap);
		List<StaffFormMap> staffFormMaps=staffMapper.getAllStaffFormMap();
		model.addAttribute("staffFormMaps", staffFormMaps);
		
		return Common.BACKGROUND_PATH + "/quality/arrangerecord/arrangerecord_edit";
	}
	
	
	@ResponseBody
	@RequestMapping(value="arrangerecord_editEntity")
	@SystemLog(module="质检安排",methods="质检安排-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		ArrangeRecordFormMap arrangeRecordFormMap=getFormMap(ArrangeRecordFormMap.class);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		
		arrangeRecordFormMap.put("arrangeDate", sdf.format(date));
		
		arrangeRecordMapper.editEntity(arrangeRecordFormMap);
		return "success";
	}
	
	
}
